package com.mall.member.service.impl;

import com.mall.member.entity.MemberLevelEntity;
import com.mall.member.exception.PhoneExsitExecption;
import com.mall.member.exception.UsernameExsitException;
import com.mall.member.service.MemberLevelService;
import com.mall.member.vo.MemberLoginVo;
import com.mall.member.vo.MemberRegisterVo;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;

import com.mall.member.dao.MemberDao;
import com.mall.member.entity.MemberEntity;
import com.mall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    private MemberLevelService memberLevelService;
    @Trace
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 完成会员注册
     */
    @Override
    public void register(MemberRegisterVo vo) {
        MemberEntity memberEntity = new MemberEntity();
        MemberLevelEntity memberLevelEntity = memberLevelService.queryMemberLevelDefault();
        memberEntity.setUsername(vo.getUserName());
        checkUsernameUnique(vo.getUserName());
        checkPhoneUnique(vo.getPhone());
        memberEntity.setMobile(vo.getPhone());
        memberEntity.setLevelId(memberLevelEntity.getId());
        memberEntity.setUsername(vo.getUserName());
        memberEntity.setNickname(vo.getUserName());
        memberEntity.setMobile(vo.getPhone());

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(vo.getPassword());
        // 需要对密码做加密处理
        memberEntity.setPassword(encode);
        // 设置其他的默认值
        this.save(memberEntity);
    }

    private void checkPhoneUnique(String phone) {
        Long count = this.lambdaQuery()
                .eq(MemberEntity::getMobile, phone)
                .count();
        if (count > 0){
            throw new PhoneExsitExecption();
        }
    }

    private void checkUsernameUnique(String userName) {
        Long count = this.lambdaQuery()
                .eq(MemberEntity::getUsername, userName)
                .count();
        if (count > 0){
            throw new UsernameExsitException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        MemberEntity entity = this.lambdaQuery()
                .eq(MemberEntity::getUsername, vo.getUserName())
                .or()
                .eq(MemberEntity::getMobile, vo.getUserName())
                .one();
        if (entity != null){
            //账号存在判断密码
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            boolean matches = bCryptPasswordEncoder.matches(vo.getPassword(), entity.getPassword());
            if (matches){
                //登录成功匹配
                return entity;
            }
        }
        return null;
    }
}